<?php 
require_once '/function/db_conn.php';
?>
<?php 
	$result = mysql_query('SELECT product_buy_id, title, b.image as image, winner, username FROM  product_buy b, member m 
		WHERE b.member_id = m.member_id AND status="F" AND winner <> "NULL" ORDER BY b.creation_time DESC LIMIT 2');
?>
<div id="left-sidebar" class="sidebar">
	<div id="tblLatestDeal-border">
		<h1 class="title" style="margin-bottom: 10px;"><span>最新成交</span></h1>
		<div class="global-content">
			<table id="tblLatestBuyer">
				<?php 
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$mbmResult = mysql_query('SELECT * FROM member WHERE member_id="' . $row["winner"] . '"');
					$mbmRow = mysql_fetch_array($mbmResult, MYSQL_ASSOC);
					$pbResult = mysql_query('SELECT count(*) as counter FROM product_buy WHERE product_buy_id="' . $row["product_buy_id"] . '"');
					$pbRow = mysql_fetch_array($pbResult, MYSQL_ASSOC);
				?>
				<tr>	
					<td colspan="2">
						<b>
							<a href="/product/sellerbid/pid/<?php echo $row["product_buy_id"];?>">
								<?php echo $row["title"];?>
							</a>
						</b>
					</td>
				</tr>
				<tr>
					<?php 
					$pos = strrpos($row["image"], "http");
					if ($pos === false) {
						list($width, $height, $type, $attr) = getimagesize("./././" . $row["image"]);
					}
					else{
						list($width, $height, $type, $attr) = getimagesize($row["image"]);
					}
					$newWidth = 0;
					$newHeight = 0;
					if($width >= $height){
						$newWidth = 50;
						$newHeight = $height * 50 / $width;
					}
					else if($height > $width){
						$newHeight = 50;
						$newWidth = $width * 50 / $height;
					}
					?>
					<td><img src="<?php echo $row["image"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img></td>
					<td>
						<a href="/member/profile/mid/<?php echo $mbmRow["member_id"];?>"><?php echo $mbmRow["username"];?></a>成功擊敗<?php echo $pbRow["counter"]?>位對手，奪得了<a href="/member/profile/mid/<?php echo $row["member_id"];?>"><?php echo $row["username"];?></a>的招標
					</td>
				</tr>
				<?php 
				}
				?>
			</table>
		</div>
	</div>
	<div id="tblLatestBuyer-border">
		<h1 class="title"><span>最新買家</span></h1>
		<div class="global-content">
			<table id="tblLatestBuyer">
				<?php 
				$result = mysql_query('SELECT product_buy_id, pb.image as image, title, country, price, day, username, mbm.member_id as member_id, sub_image1, sub_image2
					FROM  product_buy pb, member mbm WHERE pb.member_id = mbm.member_id ORDER BY pb.creation_time DESC LIMIT 5');
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				?>
				<tr>
					<td style="text-align:center;">
						<b>
							<a href="/product/sellerbid/pid/<?php echo $row["product_buy_id"];?>">
								<?php echo $row["title"];?>
							</a>
						</b>
					</td>
				</tr>
				<tr>
					<td>
						<table style="width: 100%;">
							<tr>
								<td rowspan="2" style="width: 100px; text-align: right;">
									<?php 
									$pos = strrpos($row["image"], "http");
									if ($pos === false) {
										list($width, $height, $type, $attr) = getimagesize("./././" . $row["image"]);
									}
									else{
										list($width, $height, $type, $attr) = getimagesize($row["image"]);
									}
									$newWidth = 0;
									$newHeight = 0;
									if($width >= $height){
										$newWidth = 75;
										$newHeight = $height * 75 / $width;
									}
									else if($height > $width){
										$newHeight = 75;
										$newWidth = $width * 75 / $height;
									}
									?>
									<img src="<?php echo $row["image"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
								</td>
								<td>
									<?php 
									$pos = strrpos($row["sub_image1"], "http");
									if ($pos === false) {
										list($width, $height, $type, $attr) = getimagesize("./././" . $row["sub_image1"]);
									}
									else{
										list($width, $height, $type, $attr) = getimagesize($row["sub_image1"]);
									}
									$newWidth = 0;
									$newHeight = 0;
									if($width >= $height){
										$newWidth = 37;
										$newHeight = $height * 37 / $width;
									}
									else if($height > $width){
										$newHeight = 37;
										$newWidth = $width * 37 / $height;
									}
									?>
									<img src="<?php echo $row["sub_image1"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
								</td>
							</tr>
							<tr>
								<td>
									<?php 
									$pos = strrpos($row["sub_image2"], "http");
									if ($pos === false) {
										list($width, $height, $type, $attr) = getimagesize("./././" . $row["sub_image2"]);
									}
									else{
										list($width, $height, $type, $attr) = getimagesize($row["sub_image2"]);
									}
									$newWidth = 0;
									$newHeight = 0;
									if($width >= $height){
										$newWidth = 37;
										$newHeight = $height * 37 / $width;
									}
									else if($height > $width){
										$newHeight = 37;
										$newWidth = $width * 37 / $height;
									}
									?>
									<img src="<?php echo $row["sub_image2"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>	
					<td>
						<table>
							<tr>
								<td style="text-align:center; width: 102px;"><b>限價: HKD<?php echo $row["price"];?></b></td>
								<td style="text-align:center; width: 66px;"><b>限期:<?php echo $row["day"];?>天</b></td>
							</tr>
							<tr>
								<td colspan="2" style="text-align:center;">
									用戶名:
									<a href="/member/profile/mid/<?php echo $row["member_id"];?>">
										<?php echo $row["username"];?>
									</a>
								</td>
							</tr>
							<tr>
								<td colspan="2" style="text-align:center;">指定購買地&nbsp;<img class="flag" src="/images/flag/<?php echo strtolower($row["country"]);?>.png"/></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>	
					<td><hr></td>
				</tr>
				<?php 
				}
				?>
			</table>
		</div>
	</div>
</div>